/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Alumno;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import com.sistema.gestionuniversitaria.dataLayer.AlumnoData;
import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Max
 */
public class MostrarAlumnos extends javax.swing.JInternalFrame {

    /**
     * Creates new form MostrarAlumnos
     */
    CarreraData caData = new CarreraData();
    AlumnoData alData = new AlumnoData();
    DefaultTableModel modelo = new DefaultTableModel();

    public MostrarAlumnos() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jComboCarreras.removeAllItems();
        cargarListaCarreras();
        jTbxCedula.setText("--Inhabilitado--");
        jTbxNombre.setText("--Inhabilitado--");
        btnGruModoBusqueda.add(jRadioButtonNombre);
        btnGruModoBusqueda.add(jRadioButtonCedula);
        btnGruModoBusqueda.add(jRadioButtonCarrera);
    }

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }
    }

    private void llenarTabla() throws SQLException, ExcepcionGlobal, NoDataException {

        modelo = new DefaultTableModel();
        ArrayList<Alumno> listaAlumnos = new ArrayList<Alumno>();
        if (jRadioButtonCarrera.isSelected()) {
            Carrera carrSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
            listaAlumnos = alData.getAlumnoPOrCarrera(carrSeleccionada.getCodigo());
        }
        if (jRadioButtonNombre.isSelected()) {
            listaAlumnos = alData.getAlumnoPOrNombre(jTbxNombre.getText());
        }
        if (jRadioButtonCedula.isSelected()) {
            Alumno alumno = alData.getAlumnoPOrCedula(jTbxCedula.getText().toUpperCase());
            listaAlumnos.add(alumno);
        }
        modelo.setColumnCount(0);
        modelo.addColumn("Cédula");
        modelo.addColumn("Nombre");
        modelo.addColumn("Apellidos");
        modelo.addColumn("Email");
        modelo.addColumn("Fecha de Nacimiento");
        modelo.addColumn("Teléfono");
        modelo.setNumRows(listaAlumnos.size());
        int contador = 0;
        for (Alumno al : listaAlumnos) {
            modelo.setValueAt(al.getCedula(), contador, 0);
            modelo.setValueAt(al.getNombre(), contador, 1);
            modelo.setValueAt(al.getApellidos(), contador, 2);
            modelo.setValueAt(al.getEmail(), contador, 3);
            modelo.setValueAt(al.getFechaNacimiento().toString(), contador, 4);
            modelo.setValueAt(al.getTelefono(), contador, 5);
            contador = contador + 1;
        }

        jTableAlumnos.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGruModoBusqueda = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jRadioButtonNombre = new javax.swing.JRadioButton();
        jRadioButtonCedula = new javax.swing.JRadioButton();
        jRadioButtonCarrera = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTbxNombre = new javax.swing.JTextField();
        jTbxCedula = new javax.swing.JTextField();
        jComboCarreras = new javax.swing.JComboBox();
        jBtnBuscar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableAlumnos = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Mostrar Alumnos");

        jLabel1.setText("Ingrese los terminos de búsqueda:");

        jRadioButtonNombre.setText("Nombre");
        jRadioButtonNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNombreActionPerformed(evt);
            }
        });

        jRadioButtonCedula.setText("Cédula");
        jRadioButtonCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCedulaActionPerformed(evt);
            }
        });

        jRadioButtonCarrera.setText("Carrera");
        jRadioButtonCarrera.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonCarreraActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre:");

        jLabel3.setText("Cédual:");

        jLabel4.setText("Carrera:");

        jTbxNombre.setEnabled(false);

        jTbxCedula.setEnabled(false);

        jComboCarreras.setEnabled(false);

        jBtnBuscar.setText("Buscar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        jTableAlumnos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTableAlumnos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jBtnBuscar)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButtonNombre)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonCedula)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonCarrera))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jTbxNombre))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jTbxCedula))))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButtonNombre)
                    .addComponent(jRadioButtonCedula)
                    .addComponent(jRadioButtonCarrera))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTbxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBtnBuscar)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        try {
            llenarTabla();
        } catch (SQLException ex) {
            Logger.getLogger(MostrarAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(MostrarAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(MostrarAlumnos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnBuscarActionPerformed

    private void jRadioButtonNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNombreActionPerformed
        jTbxCedula.setText("--Inhabilitado--");
        jTbxCedula.enable(false);
        jComboCarreras.enable(false);
        jTbxNombre.setText("");
        jTbxNombre.enable(true);
    }//GEN-LAST:event_jRadioButtonNombreActionPerformed

    private void jRadioButtonCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCedulaActionPerformed
        jTbxNombre.setText("--Inhabilitado--");
        jTbxNombre.enable(false);
        jComboCarreras.enable(false);
        jTbxCedula.setText("");
        jTbxCedula.enable(true);
    }//GEN-LAST:event_jRadioButtonCedulaActionPerformed

    private void jRadioButtonCarreraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonCarreraActionPerformed
        jTbxCedula.setText("--Inhabilitado--");
        jTbxCedula.enable(false);
        jComboCarreras.enable(true);
        jTbxNombre.setText("--Inhabilitado--");
        jTbxNombre.enable(false);
    }//GEN-LAST:event_jRadioButtonCarreraActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGruModoBusqueda;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButtonCarrera;
    private javax.swing.JRadioButton jRadioButtonCedula;
    private javax.swing.JRadioButton jRadioButtonNombre;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTableAlumnos;
    private javax.swing.JTextField jTbxCedula;
    private javax.swing.JTextField jTbxNombre;
    // End of variables declaration//GEN-END:variables
}
